import jdk.swing.interop.SwingInterOpUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-07-13
 * Time: 20:23
 */
//https://leetcode.cn/problems/binary-tree-preorder-traversal/submissions/644193303/
    //给你二叉树的根节点 root ，返回它节点值的 前序 遍历。(使用非递归方法)
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class Test {
    public static List<Integer> fun(TreeNode root) {
        ArrayList<Integer> list=new ArrayList();
        if(root==null){
            return list;
        }
        Stack<TreeNode> u=new Stack();
        TreeNode p=root;
        while(!u.isEmpty()||p!=null){
            while(p!=null){
                list.add(p.val);
                u.push(p);
                p=p.left;
            }
            p=u.pop();
            p=p.right;
        }
        return list;
    }
    public static void main(String[] args) {
        TreeNode A1=new TreeNode(1);
        TreeNode A2=new TreeNode(2);
        TreeNode A3=new TreeNode(3);
        TreeNode A4=new TreeNode(4);
        TreeNode A5=new TreeNode(5);
        TreeNode A6=new TreeNode(6);
        TreeNode A7=new TreeNode(7);
        A1.right=A2;
        A2.left=A3;
        ArrayList list=(ArrayList) fun(A1);
        System.out.println(list.toString());
    }
}
